package com.zut.edu;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSONObject;
import com.qmuiteam.qmui.layout.QMUILinearLayout;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.zut.edu.bean.Data;
import com.zut.edu.bean.User;
import com.zut.edu.http.BaseCallback;
import com.zut.edu.http.OkhttpHelper;
import com.zut.edu.utils.StringUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.Buffer;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    @BindView(R.id.login_button)
    QMUIRoundButton loginbutton;
    @BindView(R.id.login_register)
    ImageView loginregister;
    @BindView(R.id.login_phonetologin)
    ImageView loginphonetologin;
    @BindView(R.id.login_appeal)
    ImageView loginappeal;
    @BindView(R.id.login_phone_layout)
    QMUILinearLayout loginphonelayout;
    @BindView(R.id.login_password_layout)
    QMUILinearLayout loginpasswordlayout;
    @BindView(R.id.login_phone_text)
    EditText loginphonetext;
    @BindView(R.id.login_password_text)
    EditText loginpasswordtext;

    OkhttpHelper okhttpHelper = OkhttpHelper.getInstance();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        QMUIStatusBarHelper.translucent(this);//沉浸式
        setQMUILayout();
        setListener();

    }

    public void setListener(){
        loginbutton.setOnClickListener(this);
        loginbutton.setChangeAlphaWhenPress(true);
        loginregister.setOnClickListener(this);
        loginphonetologin.setOnClickListener(this);
        loginappeal.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_button:
                if(TextUtils.isEmpty(loginphonetext.getText().toString().trim())){
                    Toast.makeText(this,  "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(loginpasswordtext.getText().toString().trim())){
                    Toast.makeText(this,  "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                initOkhttp();
                break;
            case R.id.login_register:
                Intent loginregister = new Intent(LoginActivity.this,RegisterFirstActivity.class);
                loginregister.putExtra("type","register");
                startActivity(loginregister);
                break;
            case R.id.login_phonetologin:
                Intent loginphonetologin = new Intent(LoginActivity.this,RegisterFirstActivity.class);
                loginphonetologin.putExtra("type","tologin");
                startActivity(loginphonetologin);
                break;
            case R.id.login_appeal:
                Intent loginappeal = new Intent(LoginActivity.this,RegisterFirstActivity.class);
                loginappeal.putExtra("type","appeal");
                startActivity(loginappeal);
                break;
            default:
                break;
        }
    }
    private void setQMUILayout(){
        loginphonelayout.setBorderColor(getResources().getColor(R.color.qmui_config_color_white));
        loginphonelayout.setBorderWidth(1);
        loginphonelayout.setRadius(25);
        loginpasswordlayout.setBorderColor(getResources().getColor(R.color.qmui_config_color_white));
        loginpasswordlayout.setBorderWidth(1);
        loginpasswordlayout.setRadius(25);
    }

    public void initOkhttp(){
        Map<String,Object>params = new HashMap<>(2);
        params.put("userAccount",loginphonetext.getText().toString().trim());
        params.put("userPassword",loginpasswordtext.getText().toString().trim());
        okhttpHelper.post(StringUtils.BASE_URL+"/user/login",params, new BaseCallback<User>() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Toast.makeText(LoginActivity.this,"网络故障",Toast.LENGTH_LONG).show();
                Log.e("login_onFailure",e.getMessage());
            }

            @Override
            public void onSuccess(@NotNull Call call, @NotNull Response response, User user, int code) throws IOException {
                switch (code){
                    case 0:
                        StringUtils.postLocalObject(LoginActivity.this,user,"user");
                        StringUtils.basicInstall(LoginActivity.this);
                        switch (user.userIdentity){
                            case "2":
                                Intent login = new Intent(LoginActivity.this,MainActivity.class);
                                login.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(login);
                                finish();
                                break;
                            default:
                                Intent login2 = new Intent(LoginActivity.this,MainOpActivity.class);
                                login2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(login2);
                                finish();
                                break;
                        }

                        break;
                    case 2001:
                        Toast.makeText(LoginActivity.this,"正在审核中",Toast.LENGTH_LONG).show();
                        break;
                    default:
                        Toast.makeText(LoginActivity.this,"账号或密码错误",Toast.LENGTH_LONG).show();
                        break;
                }
                Log.e("login_response",response.code()+":"+response.message());
                Log.e("result", String.valueOf(code));
            }

            @Override
            public void onError(@NotNull Call call, @NotNull Response response, int code, Exception e) {
                Toast.makeText(LoginActivity.this,"账号或密码错误",Toast.LENGTH_LONG).show();
                if(e!=null)Log.e("login_Json_onError",e.getMessage());//Json的e判空
                Log.e("login_response_onError", String.valueOf(response.code()));
            }
        });
    }


}
